package com.wave.wavesystem.serve.controller;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.wave.common.model.DeleteRequest;
import com.wave.common.model.Result;
import com.wave.common.util.Func;
import com.wave.wavesystem.serve.enums.user.UserRoleEnum;
import com.wave.wavesystem.serve.pojo.dto.user.UserSaveDTO;
import com.wave.wavesystem.serve.pojo.entity.User;
import com.wave.wavesystem.serve.pojo.query.UserQuery;
import com.wave.wavesystem.serve.pojo.vo.UserVO;
import com.wave.wavesystem.serve.service.IUserService;
import com.wave.wavesystem.serve.util.CurrentUserUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Tag(name = "用户接口-UserController")
public class UserController {

    private final IUserService service;

    @PostMapping("/save")
    @Operation(summary = "新增")
    public Result<User> save(@RequestBody UserSaveDTO entity) {
        if (Func.isAnyEmpty(entity.getAccount(), entity.getPassword(), entity.getUsername())) {
            return Result.fail();
        }
        QueryChain<User> queryChain = service.queryChain();
        User one = queryChain.eq(User::getAccount, entity.getAccount()).one();
        if (Func.isNotEmpty(one)) {
            return Result.fail();
        }
        String salt = UUID.randomUUID().toString().substring(0, 10);
        User user = new User();
        BeanUtils.copyProperties(entity, user);
        user.setSalt(salt);
        String mdPassword = DigestUtils.md5DigestAsHex((salt + entity.getPassword()).getBytes());
        user.setPassword(mdPassword);
        service.save(user);
        if (Func.isNotEmpty(entity.getRoles())) {
            String roles = Func.strListToStr(entity.getRoles());
            service.updateChain().eq(User::getId, user.getId())
                    .set(User::getRole, roles).update();
        }
        if (Func.isEmpty(entity.getRoles())) {
            service.updateChain().eq(User::getId, user.getId())
                    .set(User::getRole, UserRoleEnum.USER.getCode()).update();
        }
        return Result.success(user);
    }

    @PostMapping("/delete")
    @Operation(summary = "通过 ids 删除")
    public Result<Void> delete(@RequestBody DeleteRequest deleteRequest) {
        service.removeByIds(Func.strToLongList(deleteRequest.getIds()));
        return Result.success();
    }

    @PostMapping("/modify")
    @Operation(summary = "修改")
    public Result<User> modify(@RequestBody UserSaveDTO entity) {
        if (Func.isEmpty(entity.getId())) {
            Long id = CurrentUserUtil.getCurrentUser().getId();
            entity.setId(id);
        }
        if (Func.isNotEmpty(entity.getRoles())) {
            entity.setRole(Func.strListToStr(entity.getRoles()));
        }
        service.updateById(entity);
        return Result.success(entity);
    }

    @PostMapping("/query")
    @Operation(summary = "根据条件查询")
    public Result<User> query(@RequestBody UserQuery query) {
        User entity = service.getById(query.getId());
        return Result.success(entity);
    }

    @PostMapping("/list")
    @Operation(summary = "列表")
    public Result<List<User>> list(@RequestBody UserQuery query) {
        QueryChain<User> queryChain = service.getQueryChain(query);
        List<User> list = service.list(queryChain);
        return Result.success(list);
    }

    @PostMapping("/page")
    @Operation(summary = "分页")
    public Result<Page<UserVO>> page(@RequestBody UserQuery query) {
        QueryChain<User> queryChain = service.getQueryChain(query);
        Page<User> page = service.page(new Page<>(query.getPageNum(), query.getPageSize()), queryChain);
        Page<UserVO> userVOPage = new Page<>();
        BeanUtils.copyProperties(page, userVOPage);
        if (Func.isNotEmpty(page.getRecords())) {
            List<UserVO> userVOS = page.getRecords().stream().map(user -> {
                UserVO userVO = new UserVO();
                BeanUtils.copyProperties(user, userVO);
                return userVO;
            }).toList();
            userVOPage.setRecords(userVOS);
        }
        return Result.success(userVOPage);
    }

}
